/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.vo.IsfdDataAnalysisVo;
import com.comac.ins.isfd.service.IIsfdDataAnalysisService;
import com.comac.ins.isfd.strategy.AnalysisCalculation;
import com.comac.ins.isfd.strategy.CalculationExampleCalculation;
import com.comac.ins.isfd.strategy.LayupCalculation;
import com.comac.ins.isfd.strategy.ModelCalculation;
import com.comac.ins.isfd.strategy.SectionCalculation;
import com.comac.ins.isfd.strategy.TableCalculationStrategy;
import lombok.RequiredArgsConstructor;


/**
 * 【数据分析】
 *
 * @author wujialin
 * @date 2024-11-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/dataAnalysis")
public class IsfdDataAnalysisController extends BaseController {

    private final IIsfdDataAnalysisService isfdDataAnalysisService;

    @Autowired
    private LayupCalculation layupCalculation;

    @Autowired
    private AnalysisCalculation analysisCalculation;

    @Autowired
    private ModelCalculation modelCalculation;

    @Autowired
    private SectionCalculation sectionCalculation;

    @Autowired
    private CalculationExampleCalculation calculationExampleCalculation;

    /**
     * 查询【铺层库】列表
     */
    @SaCheckPermission("isfd:dataAnalysis:list")
    @GetMapping("/layup/count")
    public R<List<IsfdDataAnalysisVo>> queryLayupCount(@RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                                       @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        return R.ok(isfdDataAnalysisService.queryLayupCount(startTime, endTime));
    }

    /**
     * 查询不同表的总数
     */
    @SaCheckPermission("isfd:dataAnalysis:list")
    @GetMapping("/all/count")
    public R<Map<String, Long>> queryAllCount(@RequestParam(value = "startTime", required = false)
                                              @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                              @RequestParam(value = "endTime", required = false)
                                              @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        Map<String, Long> result = new HashMap<>();

        Map<String, TableCalculationStrategy> tableCalculationStrategies = new HashMap<>();
        tableCalculationStrategies.put("analysis", analysisCalculation);
        tableCalculationStrategies.put("calculation", calculationExampleCalculation);
        tableCalculationStrategies.put("layup", layupCalculation);
        tableCalculationStrategies.put("model", modelCalculation);
        tableCalculationStrategies.put("section", sectionCalculation);

        // 遍历 tableCalculationStrategies 的所有策略，执行计算
        for (Map.Entry<String, TableCalculationStrategy> entry : tableCalculationStrategies.entrySet()) {
            String tableName = entry.getKey();
            TableCalculationStrategy strategy = entry.getValue();

            // 执行计算，获得当前表的数量
            Long count = strategy.calculate(startTime, endTime);

            result.put(tableName, count);
        }

        return R.ok(result);
    }

}
